package veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import veterinaria.excepcion.LoginExcepcion;
import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Doctor;
import veterinaria.modelo.Dueno;
import veterinaria.util.ConexionBD;

public class DoctorDAO extends BaseDAO {

	public Doctor insertar(Doctor doc) throws DAOExcepcion {
		String query;
		query = "INSERT INTO doctor ( nombre,  paterno, materno, telefono,  correo, administrador,  direccion,clave) values (?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, doc.getNombre());
			stmt.setString(2, doc.getPaterno());
			stmt.setString(3, doc.getMaterno());
			stmt.setString(4, doc.getTelefono());
			stmt.setString(5, doc.getCorreo());
			stmt.setInt(6, doc.getAdministrador());
			stmt.setString(7, doc.getDireccion());
			stmt.setString(8, doc.getClave());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return doc;

	}

	public Doctor actualizar(Doctor doc) throws DAOExcepcion {
		String query = "UPDATE doctor SET  nombre=?,  paterno=?, materno=?, telefono=?,  correo=?, administrador=?,  direccion=?,clave=? WHERE idDoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, doc.getNombre());
			stmt.setString(2, doc.getPaterno());
			stmt.setString(3, doc.getMaterno());
			stmt.setString(4, doc.getTelefono());
			stmt.setString(5, doc.getCorreo());
			stmt.setInt(6, doc.getAdministrador());
			stmt.setString(7, doc.getDireccion());
			stmt.setString(8, doc.getClave());
			stmt.setInt(9, doc.getCodigo());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return doc;

	}

	public Doctor validar(String email, String clave)
			throws DAOExcepcion, LoginExcepcion {
		String query = "select iddoctor,nombre,paterno,materno,correo from doctor where correo=? and telefono=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Doctor vo = new Doctor();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, email);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();
			System.out.println(stmt);
			
			System.out.println(rs.getRow());
			if (rs.next()) {
				vo.setCodigo(rs.getInt("iddoctor"));
				vo.setNombre(rs.getString("nombre"));
				vo.setPaterno(rs.getString("paterno"));
				vo.setMaterno(rs.getString("materno"));
				vo.setCorreo(rs.getString("correo"));

			} else {
				throw new LoginExcepcion("No existe");
			}
			
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	
	public Collection<Doctor> listar() throws DAOExcepcion {
		Collection<Doctor> c = new ArrayList<Doctor>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idDoctor,nombre,  paterno, materno, telefono,  correo, administrador,  direccion,clave from doctor order by concat(nombre ,' ',paterno ,' ', materno)";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Doctor vo = new Doctor();
				vo.setCodigo(rs.getInt("idDoctor"));
				vo.setNombre(rs.getString("nombre"));
				vo.setPaterno(rs.getString("paterno"));
				vo.setMaterno(rs.getString("materno"));
				vo.setTelefono(rs.getString("telefono"));
				vo.setCorreo(rs.getString("correo"));
				vo.setAdministrador(rs.getInt("administrador"));
				vo.setClave(rs.getString("clave"));
				vo.setDireccion(rs.getString("direccion"));

				
				c.add(vo);
				
				
				
			
				
				
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public void eliminar(int idDoctor) throws DAOExcepcion {
		String query = "delete from doctor WHERE idDoctor=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDoctor);
			int i = stmt.executeUpdate();

			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	
	
	public Doctor obtener(String atributo,Object param) throws DAOExcepcion {
		Doctor vo = new Doctor();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query ="SELECT idDoctor, nombre, paterno, materno, correo, telefono, administrador, direccion, clave FROM doctor Cliente where ";
			
			
			
			if ("CODIGO".equals(atributo))
				query = query + "idDoctor=?";
			if ("NOMBRE".equals(atributo))
				query = query + "concat(nombre ,' ',paterno ,' ', materno)=?";
			
			
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);

			
			
			if ("CODIGO".equals(atributo))
				stmt.setInt(1, (int)param);
			if ("NOMBRE".equals(atributo) || "DOCUMENTO".equals(atributo))
				stmt.setString(1, (String) param);
				
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				vo.setCodigo(rs.getInt("idDoctor"));
				vo.setNombre(rs.getString("nombre"));
				vo.setPaterno(rs.getString("paterno"));
				vo.setMaterno(rs.getString("materno"));
				vo.setTelefono(rs.getString("telefono"));
				vo.setCorreo(rs.getString("correo"));
				vo.setAdministrador(rs.getInt("administrador"));
				vo.setClave(rs.getString("clave"));
				vo.setDireccion(rs.getString("direccion"));

			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}


}
